package cn.com.gridinfo.sooc.core.course.classes.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.jeremy.arad.utils.Log;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocFragment;
import cn.com.gridinfo.sooc.core.course.classes.bean.CourseDetailsInfo;
import cn.com.gridinfo.sooc.core.course.classes.dao.CourseDao;
import cn.com.gridinfo.sooc.core.course.common.CodeConst;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 视频 fragment
 */
public class XitiFragment extends BaseSoocFragment {

    View view;
    @InjectView(R.id.course_xiti)
    WebView courseXitiView; //文档播放区域
    @InjectView(R.id.xiti_contanier)
    FrameLayout xitiContanier;//播放区域容器
    private String xitiPath;

    //课程详情 实体类
    CourseDetailsInfo courseDetailsInfo;

    //课程详情 dao
    CourseDao dao;

    //学习记录ID
    String studyId = "0" ;
    /**
     * 实例化视频Fragment
     *@param courseDetailsInfo 课程详情
     * @param path 视频路径
     * @return
     */
    public static XitiFragment newInstance(CourseDetailsInfo courseDetailsInfo, String path) {
        XitiFragment docFragment = new XitiFragment();
        Bundle args = new Bundle();
        args.putString("xitiPath", path);
        args.putSerializable("courseDetail",courseDetailsInfo);
        docFragment.setArguments(args);
        return docFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.course_xiti, null);
        ButterKnife.inject(this, view);
        //课程详情 dao
        dao = new CourseDao(this);
        //文档播放地址
        if (getArguments() != null) {
            xitiPath = getArguments().getString("xitiPath");
            //获取课程详情
            courseDetailsInfo = (CourseDetailsInfo)getArguments().getSerializable("courseDetail");
        }

        //初始化文档页面
        initDocWebView();

        return view;
    }

    // 初始化习题页面
    private void initDocWebView() {
        //设置文档网页显示，加载url
//        webview 内网页跳转
        courseXitiView.setWebViewClient(new WebViewClient(){
            //页面加载前
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
//                showProgress(true);//加载进度条
                Log.i("onPageStarted: "+ url);
                super.onPageStarted(view, url, favicon);
//                showProgress(true,true);//加载进度条
            }

            //页面加载完成
            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i("onPageFinished : "+ url);
                super.onPageFinished(view, url);
                //停止进度条网页加载完成
//                showProgress(false,true);//加载进度条
            }


            //当前加载
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                    view.loadUrl(url);

                return true;
            }

        });

        courseXitiView.setClickable(true);
        courseXitiView.getSettings().setJavaScriptEnabled(true);//允许脚本
        courseXitiView.getSettings().setPluginState(WebSettings.PluginState.ON);
        courseXitiView.getSettings().setJavaScriptEnabled(true);
        courseXitiView.getSettings().setAllowFileAccess(true);
        courseXitiView.getSettings().setDefaultTextEncodingName("GBK");
        courseXitiView.getSettings().setAppCacheEnabled(false);
        courseXitiView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 隐藏缩放图片
        courseXitiView.getSettings().setDisplayZoomControls(false);// 显示放大缩小
        courseXitiView.getSettings().setAllowFileAccess(true);
        // 默认缩放模式
        courseXitiView.getSettings().setUseWideViewPort(true);// 无限缩放
//        courseDocView.getSettings().setLoadWithOverviewMode(true);//加载页面的到屏幕大小模式
        courseXitiView.loadUrl( xitiPath);//加载文档地址
    }

    /**
     * 请求成功,请求成功
     */
    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.status.equals("0")) {
                switch (requestCode) {
                    //记录学习行为
                    case CodeConst.COURSE_SETBEHAVIOR_START:
                        studyId = dao.studyId;//记录学习行为ID
                        break;

                }
            } else {
                MessageUtils.showImageShortToast(getActivity(), dao.statusCode);
            }

        } catch (Exception e) {
//            Log.e(e.getMessage(), e);
//            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        //记录当前播放内容开始学习行为,并且不是课程老师
        if (courseDetailsInfo.getKcls() == 0) {
            dao.setBehavior(courseDetailsInfo.getKcid(), courseDetailsInfo.getCurrPmId(), courseDetailsInfo.getCurrMlId(), courseDetailsInfo.getCurrNrId(), "0",0);
            Log.d("播放记录开始记录..."+courseDetailsInfo.getCurrNrId());
        }

    }

    @Override
    public void onPause() {
        super.onPause();

        //结束学习记录记录学习行为
        if (!studyId.equals("0")) {
            //并且不是课程老师
            if (courseDetailsInfo.getKcls() == 0) {
                dao.setBehavior(courseDetailsInfo.getKcid(), courseDetailsInfo.getCurrPmId(), courseDetailsInfo.getCurrMlId(), courseDetailsInfo.getCurrNrId(), studyId, 0);
                Log.d("播放记录记录停止..."+courseDetailsInfo.getCurrNrId());
            }
        }
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
